#!/usr/bin/python

import socket
import sys

if len(sys.argv) != 3:
  print "USAGE : ./liveclient.py <port_out> <port_in>"
  sys.exit(1)

port_out = int(sys.argv[1])
port_in = int(sys.argv[2]) 

sock_out = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock_out.connect(("localhost", port_out))

sock_serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock_serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock_serv.bind(("", port_in))
sock_serv.listen(1)

sock_out.send("QSSessionNew(live)\n")
ret = sock_out.recv(1024).strip()

sock_out.send("QSSessionPort("+str(port_in)+")\n")
ret = sock_out.recv(1024).strip()

print ret

sock_in = sock_serv.accept()[0]

ret = " "
while ret:
  cmd = sys.stdin.readline()
  if not cmd:
    break
  sock_in.send(cmd + "\n")
  ret = sock_in.recv(1024).strip()
  print ret
while ret:
  ret = sock_in.recv(1024).strip()
  print ret
